package github.kawaiior.yuanchuxiuzhen.common.gongfa.base;

import github.kawaiior.yuanchuxiuzhen.common.block.ore.ManaStoneOre;
import github.kawaiior.yuanchuxiuzhen.common.capability.entity.ManaPower;
import github.kawaiior.yuanchuxiuzhen.common.gongfa.GongFaBase;
import github.kawaiior.yuanchuxiuzhen.core.init.GongFaInit;
import github.kawaiior.yuanchuxiuzhen.core.registry.ModCapability;
import net.minecraft.block.Block;
import net.minecraft.block.OreBlock;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.potion.EffectInstance;
import net.minecraft.potion.Effects;
import net.minecraft.world.World;
import net.minecraftforge.client.event.RenderPlayerEvent;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.event.entity.living.LivingHurtEvent;

import java.util.HashMap;
import java.util.Map;

public class JinGuangXuanGong extends GongFaBase {
    public JinGuangXuanGong(String itemName) {
        super(ManaPower.ManaRoot.METAL, 5000, GongFaInit.JIN_YAO_JUE, itemName);
    }


    @Override
    public void onPlayerLearn(PlayerEntity player, World world, ManaPower power) {

    }

    @Override
    public void onPlayerLearnFinish(PlayerEntity player, World world, ManaPower power) {
        // TODO: 添加效果

    }

    public static void onPlayerDestroyBlock(PlayerEntity player, World world, Block block, ManaPower power){
        learn(player, world, block, power);
    }

    private static void learn(PlayerEntity player, World world, Block block, ManaPower power){
        if (!power.isLearningGongFa(GongFaInit.JIN_GUANG_XUAN_GONG)){
            return;
        }

        int experience;
        if (block instanceof OreBlock){
            experience = 1;
        }else if (block instanceof ManaStoneOre){
            experience = 2 * ((ManaStoneOre)block).getLevel();
        }else {
            return;
        }

        power.addGongFaExp(player, world, experience);
    }


    // 以下为效果
    public static class InvisibilityState{
        private boolean isInvisibility = false;
        private int invisibilityTick = 0;

        public boolean isInvisibility() {
            return isInvisibility;
        }

        public void setInvisibility(boolean invisibility) {
            isInvisibility = invisibility;
        }

        public int getInvisibilityTick() {
            return invisibilityTick;
        }

        public void setInvisibilityTick(int invisibilityTick) {
            this.invisibilityTick = invisibilityTick;
        }

        public void tick(){
            System.out.println(this.invisibilityTick);
            this.invisibilityTick++;
        }
    }

    private static Map<PlayerEntity, InvisibilityState> playerInvisibilityMap = new HashMap<>();

    private static InvisibilityState getPlayerInvisibilityState(PlayerEntity player){
        if (!playerInvisibilityMap.containsKey(player)){
            playerInvisibilityMap.put(player, new InvisibilityState());
        }
        return playerInvisibilityMap.get(player);
    }

    public static boolean isPlayerInvisibility(PlayerEntity player){
        return getPlayerInvisibilityState(player).isInvisibility();
    }

    public static void tick(PlayerEntity player, World world, ManaPower power){
        if (!power.isLearnedGongFa(GongFaInit.JIN_GUANG_XUAN_GONG) || world.isRemote){
            return;
        }

        InvisibilityState is = getPlayerInvisibilityState(player);

        if (player.isSneaking() && power.consumeMana(player, world, 100)){
            if (!is.isInvisibility()){
                is.setInvisibility(true);
                is.setInvisibilityTick(0);
            }

            is.tick();
            player.addPotionEffect(new EffectInstance(Effects.INVISIBILITY, 60));
        }else {
            is.setInvisibility(false);
        }
    }

    public static void onPlayerRender(PlayerEntity player, World world, RenderPlayerEvent event){
        if (isPlayerInvisibility(player) && !player.isGlowing()){
            if (event.isCancelable()){
                event.setCanceled(true);
            }
        }
    }

    public static void onPlayerAttack(PlayerEntity player, LivingHurtEvent event){
        InvisibilityState is = getPlayerInvisibilityState(player);
        if (is.getInvisibilityTick() >= 10){
            event.setAmount(event.getAmount()*3F);
        }
        is.setInvisibilityTick(0);
    }
}
